<?php

	/**
	* Return data when called by ajax
	* return in json format
	* 
	* @author trungdt
	* @since Nov 4, 2011
	* 
	* Change logs
	*/

	if (!defined('EXPONENT')) exit('');

	$locsql = "({$db->prefix}calendar.location_data='".serialize($loc)."'";
	$config = $db->selectObject("calendarmodule_config","location_data='".serialize($loc)."'");
	if (!empty($config->aggregate)) {
		$locations = unserialize($config->aggregate);
		foreach ($locations as $source) {
			$tmploc = null;
			$tmploc->mod = 'calendarmodule';
			$tmploc->src = $source;
			$tmploc->int = '';
			$locsql .= " OR {$db->prefix}calendar.location_data='".serialize($tmploc)."'";
		}
	}
	$locsql .= ')';

	$permission_approve=exponent_permissions_check(array("administrate", "approve"), $loc);
	$edit_post=exponent_permissions_check(array("edit"), $loc);
	$delete_post=exponent_permissions_check(array("delete"), $loc);

	$sql_where=" AND (approve = 2)";
	if ($permission_approve)
	{
		$sql_where="";
	}

	// for from_date ; to_date
	// calculate clear date and from date
	// default: today
	$from_date=calendarmodule::DateClearTime(aboid_getCurrentTime());
	$to_date=$from_date+24*3600-1;
	if (isset($_REQUEST['from_date']))
	{
		$from_date=_ab_safe_html_string($_REQUEST['from_date']);
		$from_date=str_replace(";","",$from_date); // for sql injection :)
	}

	if (isset($_REQUEST['to_date']))
	{
		$to_date=_ab_safe_html_string($_REQUEST['to_date']);
		$to_date=str_replace(";","",$to_date); // for sql injection :)
	}

	$sql_where.=" AND (
	((eventstart >= {$from_date}) AND (eventstart<={$to_date}))
	OR ((eventend >= {$from_date}) AND (eventend<={$to_date})) 
	OR ((eventstart <= {$from_date}) AND (eventend >= {$to_date}))
	)";

	$data=null;


	$time=aboid_getCurrentTime(); // get current time by time belt
	$current_day_of_week=date("w", $time);

	$sql="SELECT {$db->prefix}calendar.*, {$db->prefix}category.name category , {$db->prefix}category.color cat_color
	FROM 
	{$db->prefix}calendar INNER JOIN 
	{$db->prefix}category
	ON 
	({$db->prefix}calendar.location_data = {$db->prefix}category.location_data) AND ({$db->prefix}calendar.category_id = {$db->prefix}category.id)

	WHERE {$locsql} {$sql_where} 
	ORDER BY eventstart
	";

	$data=$db->selectObjectsBySql($sql);

	//$data=$db->selectObjects("calendar", "location_data = '{$sloc}' ORDER BY approve");

	$data_ret=array();

	foreach ($data as $k=>$v){			
		// for permission set
		$v->permission_edit=($edit_post)?1:0;
		$v->permission_delete=($delete_post)?1:0;
		if ($config->need_permission == 1 && !$permission_approve && ($config->location_data == $v->location_data)){
			$v->title="Đã được thuê";
			$v->body="";
		}

		if ($config->show_cat_whenajax == 0)
			$v->category="";

		//
		$v->fhour=date("G", $v->eventstart);
		$v->thour=date("G", $v->eventend);
		//
		$v->fmin=date("i", $v->eventstart);
		$v->tmin=date("i", $v->eventend);

		//
		$temp=exponent_users_getUserById($v->poster);
		$v->poster_name="{$temp->firstname} {$temp->lastname}({$temp->username})";
		//
		$v->real_id=$v->id;

		// because we start the calendar by Monday
		// php return 0 for sunday
		// for not repeat
		if ($v->repeat_type  == -1){
			// event start
			if (($v->eventstart>=$from_date) && ($v->eventstart<=$to_date))
			{
				$v->eventend=mktime(date("G", $v->eventend),date("i", $v->eventend),date("s", $v->eventend),date("n",$v->eventstart),date("j",$v->eventstart),date("Y",$v->eventstart));
				$v->full_date=_ab_dateFormat($v->eventstart,false,true);
				$v->day_of_week=date("w", $v->eventstart);
				$v->day_start=date("j", $v->eventstart);
				if ($v->day_of_week == 0)
					$v->day_of_week=6;
				else
					$v->day_of_week--;

				$data_ret[]=clone $v;
			}
			continue;
		}
		// for repeat daily
		if ($v->repeat_type == 0)
		{
			// determine $from_date - todate of this event
			$tfrom_date=mktime(date("G", $v->eventstart),date("i", $v->eventstart),date("s", $v->eventstart),date("n",$from_date),date("j",$from_date),date("Y",$from_date));
			if ($from_date< $v->eventstart)
			{
				$tfrom_date=$v->eventstart;
			}
			$tto_date=mktime(date("G", $v->eventstart),date("i", $v->eventstart),date("s", $v->eventstart),date("n",$to_date),date("j",$to_date),date("Y",$to_date));
			if ($v->eventend < $to_date )
			{
				$tto_date = $v->eventend;
			}
			$current_date=$tfrom_date;
			do { 
				// loop through from_date to to_date
				// get current day
				$current_day=date("j",$current_date);
				// event start
				$v->eventstart=$current_date;
				$v->eventend=mktime(date("G", $v->eventend),date("i", $v->eventend),date("s", $v->eventend),date("n",$v->eventstart),date("j",$v->eventstart),date("Y",$v->eventstart));
				$v->day_start=$current_day;
				$v->full_date=_ab_dateFormat($current_date,false,true);
				$v->day_of_week=date("w", $current_date);
				if ($v->day_of_week == 0)
					$v->day_of_week=6;
				else
					$v->day_of_week--;

				// add event
				$v->id=$v->real_id."_".$current_day;
				$data_ret[]=clone $v;
				// increase 1 day
				$current_date=strtotime("+1 day", $current_date);
			} while ($current_date <= $tto_date);
			continue;
		}

		// for weekly repeat
		if ($v->repeat_type == 1)
		{
			// determine $from_date - todate of this event
			$tfrom_date=mktime(date("G", $v->eventstart),date("i", $v->eventstart),date("s", $v->eventstart),date("n",$from_date),date("j",$from_date),date("Y",$from_date));
			if ($from_date< $v->eventstart)
			{
				$tfrom_date=$v->eventstart;
			}
			$tto_date=mktime(date("G", $v->eventstart),date("i", $v->eventstart),date("s", $v->eventstart),date("n",$to_date),date("j",$to_date),date("Y",$to_date));
			if ($v->eventend < $to_date )
			{
				$tto_date = $v->eventend;
			}
			$current_date=$tfrom_date;
			$t_day_ofweek=date("w", $v->eventstart);
			do { 
				if (date("w", $current_date) == $t_day_ofweek)
				{
					// get current day
					$current_day=date("j",$current_date);
					// event start
					$v->eventstart=$current_date;
					$v->eventend=mktime(date("G", $v->eventend),date("i", $v->eventend),date("s", $v->eventend),date("n",$v->eventstart),date("j",$v->eventstart),date("Y",$v->eventstart));
					$v->day_start=$current_day;
					$v->full_date=_ab_dateFormat($current_date,false,true);
					$v->day_of_week=date("w", $current_date);
					if ($v->day_of_week == 0)
						$v->day_of_week=6;
					else
						$v->day_of_week--;

					// add event
					$v->id=$v->real_id."_".$current_day;
					$data_ret[]=clone $v;
				}

				// increase 1 day
				$current_date=strtotime("+1 day", $current_date);
			} while ($current_date <= $tto_date);
			continue;	
		}
		// for monthly repeat
		if ($v->repeat_type ==2)
		{
			$test_array=array();
			$test_array[]=mktime(date("G", $v->eventstart),date("i", $v->eventstart),date("s", $v->eventstart),date("n",$from_date),date("j",$v->eventstart),date("Y",$from_date));
			if (date("n", $from_date) != date("n", $to_date))
				$test_array[]=mktime(date("G", $v->eventstart),date("i", $v->eventstart),date("s", $v->eventstart),date("n",$to_date),date("j",$v->eventstart),date("Y",$to_date));

			foreach ($test_array as $k) 
				if (($k>=$from_date) && ($k<=$to_date))
				{
					$current_date=$k;
					// get current day
					$current_day=date("j",$current_date);
					// event start
					$v->eventstart=$current_date;
					$v->eventend=mktime(date("G", $v->eventend),date("i", $v->eventend),date("s", $v->eventend),date("n",$current_date),date("j",$current_date),date("Y",$current_date));
					$v->day_start=$current_day;
					$v->full_date=_ab_dateFormat($current_date,false,true);
					$v->day_of_week=date("w", $current_date);
					if ($v->day_of_week == 0)
						$v->day_of_week=6;
					else
						$v->day_of_week--;

					// add event
					$v->id=$v->real_id."_".$current_day;
					$data_ret[]=clone $v;
				}

		}
		// yearly repeat
		if ($v->repeat_type ==3){
			$test_array=array();
			$test_array[]=mktime(date("G", $v->eventstart),date("i", $v->eventstart),date("s", $v->eventstart),date("n",$v->eventstart),date("j",$v->eventstart),date("Y",$from_date));
			if (date("Y", $from_date) != date("Y", $to_date))
				$test_array[]=mktime(date("G", $v->eventstart),date("i", $v->eventstart),date("s", $v->eventstart),date("n",$v->eventstart),date("j",$v->eventstart),date("Y",$to_date));

			foreach ($test_array as $k) 
				if (($k>=$from_date) && ($k<=$to_date))
				{
					$current_date=$k;
					// get current day
					$current_day=date("j",$current_date);
					// event start
					$v->eventstart=$current_date;
					$v->eventend=mktime(date("G", $v->eventend),date("i", $v->eventend),date("s", $v->eventend),date("n",$current_date),date("j",$current_date),date("Y",$current_date));
					$v->day_start=$current_day;
					$v->full_date=_ab_dateFormat($current_date,false,true);
					$v->day_of_week=date("w", $current_date);
					if ($v->day_of_week == 0)
						$v->day_of_week=6;
					else
						$v->day_of_week--;

					// add event
					$v->id=$v->real_id."_".$current_day;
					$data_ret[]=clone $v;
				}
		}
	}
	// prepare for return json
	$ret=null;
	$ret->total=count($data_ret);
	$ret->rows=$data_ret;
	echo json_encode($ret);
?>
